package com.example.mymusic.app;

import static com.example.mymusic.utils.BluetoothController.bluetoothAdapter;

import android.Manifest;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.example.mymusic.utils.Comm;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.List;

//前台服务    没有用好像。。
public class ForegroundService extends Service {

    final String TAG = "前台服务";
    private static final int NOTIFICATION_ID = 1;
    private static final String CHANNEL_ID = "music_channel";

    //starService方式启动，这里是步骤2
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: ");

        // 创建通知
        Notification notification = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            notification = new Notification.Builder(this, CHANNEL_ID)
                    .setContentTitle("音乐播放器")
                    .setContentText("正在播放音乐")
                    .build();
        }

        // 开启前台服务
//        startForeground(NOTIFICATION_ID, notification);

        return super.onStartCommand(intent, flags, startId);
    }

    //先执行这里 步骤1
    @Override
    public void onCreate() {
        super.onCreate();

        // 创建通知渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Music Channel", NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            manager.createNotificationChannel(channel);
        }

        registerActivityLifecycleCallbacks();
    }


    private void registerActivityLifecycleCallbacks() {
        Log.d(TAG, "registerActivityLifecycleCallbacks: ");

        Application application = (Application) getApplicationContext();
        application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
                Log.d(TAG, "onActivityCreated:111111111111111 ");
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {
                Log.d(TAG, "onActivityCreated: 22222222222222222");
            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
                Log.d(TAG, "onActivityCreated: 33333333333333333");
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {
                Log.d(TAG, "onActivityCreated: 4444444444444444444");
            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                Log.d(TAG, "onActivityCreated: 5555555555555");
            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {
                Log.d(TAG, "onActivityCreated: 666666666666666");
            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {
                Log.d(TAG, "onActivityCreated:777777777777777 ");
                bluetoothDisable();
            }
        });

    }


    public void bluetoothDisable(){
        BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
        Log.d(TAG, "bluetoothDisable:  蓝牙连接状态"+bluetooth.isEnabled());
        if (bluetoothAdapter.isEnabled()){
            if (ActivityCompat.checkSelfPermission(this,Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                Log.d(TAG, "bluetoothDisable: 111111111111111");return;}
            Log.d(TAG, "bluetoothDisable: 2222222222222222222222");

            //手机没有root权限没法使用
            Process runtime;
            try {
                runtime = Runtime.getRuntime().exec("su");
                PrintWriter pw = new PrintWriter(runtime.getOutputStream());
                pw.println("svc bluetooth disable");
                //pw.println("am start -n com.example.jdl3/"+ActivityId);
                //pw.write("am start -n ${AppId}/${AppActivityId}");
                pw.flush();
                pw.close();
                onDestroy();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    public Boolean runOrStop(){
        return Comm.getInt(Comm.FlagKey)==2;

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
